package com.xl.rentkeeper.act;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.xl.fuse.protos.viewroom.RoomFilter;
import com.xl.fuse.protos.viewroom.RoomInfo;
import com.xl.fuse.protos.viewroom.RoomStatus;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.business.OrderLogic;
import com.xl.rentkeeper.util.TimeUtils;
import com.xl.rentkeeper.util.TipMapping;

import java.util.ArrayList;
import java.util.List;

public class SearchRoomInfoAct extends BaseAct implements AdapterView.OnItemClickListener, View.OnClickListener {
    private EditText mETKey;
    private ListView mLVRoominfo;
    private List<RoomInfo> mList = new ArrayList<>();
    private SearchRoomInfoAdapter mAdapter;
    private TextView mTVCancel;
    private int mPosition;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_room_info);
        mPosition = getIntent().getIntExtra("position",0);
        mETKey = (EditText) findViewById(R.id.et_key);
        mTVCancel = (TextView) findViewById(R.id.tv_cancel);
        mTVCancel.setOnClickListener(this);
        mETKey.addTextChangedListener(new ChangeListener());
        mLVRoominfo = (ListView) findViewById(R.id.lv_roominfo);
        mAdapter = new SearchRoomInfoAdapter(getLayoutInflater(),mList);
        mLVRoominfo.setAdapter(mAdapter);
        mLVRoominfo.setOnItemClickListener(this);
    }

    @Override
    public String[] monitorEvents() {
        return new String[]{PMS_LOGIC_RoomSerach};
    }

    private void searchRoom(String key){
        if(TextUtils.isEmpty(key)){
            mList.clear();
            mAdapter.notifyDataSetChanged();
            return;
        }
        RoomFilter filter = new RoomFilter.Builder().name(key).build();
        OrderLogic.getInstance().searchRoomInfo(filter);
    }

    @Override
    public void onEvent(String event, boolean ret, String errInfo, Object[] data) {
        super.onEvent(event, ret,errInfo, data);
        if(PMS_LOGIC_RoomSerach.equals(event)){
            if(ret){
                if(data!=null && data.length>0 && data[0]!=null){
                    List<RoomInfo> rooms = (List<RoomInfo>)data[0];
                    mList.clear();
                    mList.addAll(rooms);
                    mAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        RoomInfo info = mList.get(position);
        Intent intent  = new Intent();
        intent.putExtra("info",info);
        intent.putExtra("position",mPosition);
        setResult(1, intent);
        finish();
    }

    @Override
    public void onClick(View v) {
        if(v==mTVCancel){
            finish();
        }
    }

    class ChangeListener implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String key = mETKey.getText().toString().trim();
            searchRoom(key);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
    class SearchRoomInfoAdapter extends BaseAdapter{
        LayoutInflater inflater;
        List<RoomInfo> list;
        public SearchRoomInfoAdapter(LayoutInflater inflater, List<RoomInfo> list){
            this.inflater = inflater;
            this.list = list;
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            RoomInfo info = list.get(position);
            ViewHolder viewHolder;
            if(convertView == null){
                viewHolder = new ViewHolder();
                convertView = inflater.inflate(R.layout.search_roominfo_item,null);
                viewHolder.tv_addr = (TextView) convertView.findViewById(R.id.tv_addr);
                viewHolder.tv_status = (TextView) convertView.findViewById(R.id.tv_status);
                viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            if(info.name!=null){
                viewHolder.tv_addr.setText(info.name);
            }
            if(info.status!=null){
                if(((int)info.status) == ((int) RoomStatus.STATUS_ORDERED.getValue()) && info.contractEndTime!=null && info.contractEndTime!=0)
                    viewHolder.tv_status.setText(TipMapping.getRoomStatus(info.status)+"(到租时间: "+ TimeUtils.fmtDateToPoint(info.contractEndTime)+")");
                else
                    viewHolder.tv_status.setText(TipMapping.getRoomStatus(info.status));
            }
            if(info.price!=null){
                viewHolder.tv_price.setText(" ￥" + info.price);
            }else{
                viewHolder.tv_price.setText("");
            }
            return convertView;
        }
    }
    class ViewHolder{
        TextView tv_addr;
        TextView tv_status;
        TextView tv_price;
    }

}
